package cn.itcast.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.beans.PropertyDescriptor;
import java.time.LocalDateTime;

@Component
@Aspect
public class AddUpdateTimeAspect {
    @Around("@annotation(cn.itcast.annotation.UpdateAnnotation)")
    public Object enhanceUpdate(ProceedingJoinPoint pjp) throws Throwable {
        Object[] args = pjp.getArgs();
        Object obj = args[0];
        Class<?> clazz = obj.getClass();
        new PropertyDescriptor("updateTime", clazz).getWriteMethod().invoke(obj, LocalDateTime.now());
        Object proceed = pjp.proceed();
        return proceed;
    }
}
